package com.yw.smartcityforjava.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yw.smartcityforjava.App;
import com.yw.smartcityforjava.R;
import com.yw.smartcityforjava.activity.UserInfoActivity;
import com.yw.smartcityforjava.api.ICallback;
import com.yw.smartcityforjava.api.ServiceCreator;
import com.yw.smartcityforjava.api.user.UserInfoRes;

public class PersonFragment extends Fragment implements View.OnClickListener{

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView headIv;
    private TextView accountTv;
    private TextView userInfoTv;
    private TextView modifyPwdTv;
    private TextView orderListTv;
    private TextView suggestTv;
    private Button logoutBt;

    public PersonFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_person, container, false);

        toolbar = (Toolbar) root.findViewById(R.id.toolbar);
        titleTv = (TextView) root.findViewById(R.id.titleTv);
        headIv = (ImageView) root.findViewById(R.id.headIv);
        accountTv = (TextView) root.findViewById(R.id.accountTv);
        userInfoTv = (TextView) root.findViewById(R.id.userInfoTv);
        modifyPwdTv = (TextView) root.findViewById(R.id.modifyPwdTv);
        orderListTv = (TextView) root.findViewById(R.id.orderListTv);
        suggestTv = (TextView) root.findViewById(R.id.suggestTv);
        logoutBt = (Button) root.findViewById(R.id.logoutBt);

        titleTv.setText("个人中心");
        userInfoTv.setOnClickListener(this);
        modifyPwdTv.setOnClickListener(this);
        orderListTv.setOnClickListener(this);
        suggestTv.setOnClickListener(this);
        logoutBt.setOnClickListener(this);


        loadUserInfo();

        return root;
    }

    private void loadUserInfo() {
        ServiceCreator.getInstance().getApi().loadUserInfo(App.token).enqueue(new ICallback<UserInfoRes>() {
            @Override
            public void onSuccess(UserInfoRes response) {
                UserInfoRes.UserBean user = response.getUser();
                //保存个人信息
                App.userInfo = user;
                accountTv.setText(user.getUserName());
                if(!TextUtils.isEmpty(user.getAvatar())){
                    Glide.with(getContext()).load(App.baseUrl+user.getAvatar()).circleCrop().into(headIv);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.userInfoTv:
                startActivity(new Intent(getContext(), UserInfoActivity.class));
            break;
            case R.id.modifyPwdTv:

                break;
            case R.id.orderListTv:

                break;
            case R.id.suggestTv:

                break;
            case R.id.logoutBt:

                break;
            default:

                break;
        }
    }
}